package com.android.lovely.ui.fragment

import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.android.lovely.R
import com.android.lovely.const.CreatedRoleRefresh
import com.android.lovely.const.HomeShowRole
import com.android.lovely.const.HomeShowTask
import com.android.lovely.databinding.FragmentWorkRoleBinding
import com.android.lovely.framework.BaseFragment
import com.android.lovely.ui.dialog.CreateImageTypeDialog
import com.android.lovely.ui.dialog.CreateRoleTypeDialog
import com.android.lovely.ui.fragment.role.CreateRoleStep1Fragment
import com.android.lovely.ui.fragment.role.CreateRoleStep2Fragment
import com.android.lovely.ui.fragment.role.CreateRoleStep3Fragment
import com.android.lovely.ui.fragment.role.CreateRoleStep4Fragment
import com.android.lovely.ui.fragment.role.CreateRoleStep5Fragment
import com.android.lovely.ui.viewmodel.WorkCreateViewModel
import com.android.lovely.ui.viewmodel.WorkTaskViewModel
import com.google.android.play.integrity.internal.n
import com.kotlin.extensions.bus.FlowBus
import com.kotlin.extensions.bus.flowOf
import com.kotlin.extensions.coroutine.CoroutineExt.launchSafety
import com.kotlin.extensions.view.ViewExt.clickable
import kotlinx.coroutines.launch

/**
 *
 * @author why
 * @date 2025/4/8 16:03
 */
class WorkFragment: BaseFragment<FragmentWorkRoleBinding>() {

    val viewModel by activityViewModels<WorkCreateViewModel>()
    val taskViewModel by activityViewModels<WorkTaskViewModel>()

    private val roleTabsType by lazy {
        arrayOf(getString(R.string.all), getString(R.string.in_review), getString(R.string.be_rejected), getString(R.string.passed))
    }

    private val roleTabs by lazy {
        arrayOf(getString(R.string.public_desc), getString(R.string.private_desc), getString(R.string.draft))
    }

    private val taskTabsType by lazy {
        arrayOf(getString(R.string.all), getString(R.string.passed), getString(R.string.checking), getString(R.string.failed))
    }

    var currentPosition = 0

    override fun initView() {
        mBinding.vp2.isUserInputEnabled = false
        mBinding.vp2.adapter = FragmentAdapter()
        mBinding.vp2.offscreenPageLimit = 2
        mBinding.vp2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                selectPosition(position)
            }
        })
        val task = arguments?.getBoolean("task", false)
        val select = if(task == true) 1 else 0
        mBinding.vp2.currentItem = select
        mBinding.tvRole.clickable {
            mBinding.vp2.currentItem = 0
        }
        mBinding.tvTask.clickable {
            mBinding.vp2.currentItem = 1
        }
        mBinding.tvRoleSelect.clickable {
            if(currentPosition == 0) {
                CreateRoleTypeDialog().show(fragmentManager = childFragmentManager)
            } else {
                CreateImageTypeDialog().show(fragmentManager = childFragmentManager)
            }
        }
        lifecycleScope.launchSafety{
            viewModel.roleType.observe(this@WorkFragment){
                mBinding.tvRoleSelect.text = if(currentPosition == 0) {
                    "${roleTabs[it.first]}-${roleTabsType[it.second]}".also { mBinding.tvRoleSelect.text = it }
                } else {
                    taskTabsType[when(taskViewModel.status){
                        1 -> 1
                        0 -> 2
                        null -> 0
                        else -> 3
                    }]
                }
            }
        }
        lifecycleScope.launchSafety{
            taskViewModel.statueType.observe(this@WorkFragment){
                mBinding.tvRoleSelect.text = if(currentPosition != 0) {
                    taskTabsType[when(taskViewModel.status){
                        1 -> 1
                        0 -> 2
                        null -> 0
                        else -> 3
                    }]
                } else {
                    mBinding.tvRoleSelect.text
                }
            }
        }
        viewLifecycleOwner.lifecycleScope.launch {
            FlowBus.flowOf<HomeShowRole>().collect {
                selectPosition(0)
                mBinding.vp2.currentItem = 0
            }
        }
        viewLifecycleOwner.lifecycleScope.launch {
            FlowBus.flowOf<HomeShowTask>().collect {
                selectPosition(1)
                mBinding.vp2.currentItem = 1
            }
        }
    }

    fun selectPosition(position: Int) {
        currentPosition = position
        mBinding.tvRole.isSelected = position == 0
        mBinding.tvTask.isSelected = position == 1
        mBinding.tvRoleSelect.text = if(currentPosition == 0) {
            "${roleTabs[viewModel.roleType.value?.first ?: 0]}-${roleTabsType[viewModel.roleType.value?.second ?: 0]}".also { mBinding.tvRoleSelect.text = it }
        } else {
            taskTabsType[when(taskViewModel.status){
                1 -> 1
                0 -> 2
                null -> 0
                else -> 3
            }]
        }
    }

    override suspend fun initData() {

    }

    private inner class FragmentAdapter : FragmentStateAdapter(this) {

        override fun getItemCount(): Int {
            return 2
        }

        override fun createFragment(position: Int): Fragment {
            return when (position) {
                0 -> WorkRoleFragment()
                else -> WorkTaskFragment()
            }
        }
    }
}